<?php ob_start(); ?>
<link rel="stylesheet" type="text/css" href="public/css/css-edit.css">
<?php 
require_once"libraries/utilites/functions.php";
$path = "data/films.txt";
$full_path = realpath($path);
$fimls = mreadFile($full_path);
?>
<div id="khung">
    <table border="0">
                <?php if (isset($_GET["if"])) { ?>
            <form method="post" action="edit.php?if=<?php echo $_GET["if"]; ?>">
                <?php foreach ($fimls as $fiml) { ?>
                    <?php $b = trim($fiml, "\t\n\r "); ?>
                    <?php $mang = explode(";", $fiml); ?>
                    <?php if ($_GET["if"] == $mang[0]) { ?>
                            <tr>
                                <td class="name">NAME :</td><td class="rating">RATING :</td>
                            </tr>
                            <tr>
                                <td height="40px"><input type="text" name="fiml-title" id="fiml-title" value="<?php if (isset($mang[2])) echo $mang[2]; ?>"></td>
                                <td><input type="text" name="fiml-rating" id="fiml-rating" value="<?php if (isset($mang[3])) echo $mang[3]; ?>"></td>
                            </tr>
                            <tr>
                                <td colspan="2" class="dicrection">
                                    <img src="public/images/films/<?php echo $mang[1]; ?>" align="left">
                                    <textarea name="fiml-dicrection"cols="45" rows="4" placeholder="Dicretion..."> <?php if(isset($mang[4])) echo $mang[4]; ?></textarea>
                                </td>
                            <tr>
                                <td class="edit" colspan="2"><input type="submit" name="submit"  value="Save"></td>    
                            </tr>
                    <?php } ?>
                <?php } ?>
            </form>
        </table>
    <?php } ?>

<?php 
if(isset($_POST["submit"]))
{
 $index=0;
 echo $_POST["fiml-dicrection"]; 
    for ($i = 0; $i < count($fimls); $i++) {
        $b = trim($fimls[$i], "\t\n\r");
        if ($b != "") {
            $fimls[$i] = explode(";", $b);
            
            if ($_GET["if"] == $fimls[$i][0]) {
                $fimls[$i][2] = $_POST["fiml-title"];
                $fimls[$i][3] = $_POST["fiml-rating"];
                if(!empty($_POST["fiml-dicrection"]))
                {
                if(isset($fimls[$i][4]))
                $fimls[$i][4]=trim($_POST["fiml-dicrection"]);
                else
                $fimls[$i][]=trim($_POST["fiml-dicrection"]);
                $index=$fimls[$i][0]; 
                }
            }
        }
        else
            $fimls[$i] = $b;
    }
  
   inputfile($fimls);
   header("location:index.php?id=".$index);
}
?>
</div>